package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_products;
import cn.com.api.entity.ApiT_specification;

import cn.com.api.service.ApiSmslogService;
import cn.com.api.service.ApiT_productsService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "products/")
public class ApiT_productsController {

	@Autowired
	private ApiT_productsService apiT_productsService;
	@Autowired
	private ApiSmslogService apiSmslogService;
	//
	private static Logger logger = Logger
			.getLogger(ApiT_productsController.class);

	/**
	 * insert
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "insertT_products")
	public void insertT_products(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String goods_id = request.getParameter("goods_id");
		String price = request.getParameter("price");
		String unit = request.getParameter("unit");
		String store = request.getParameter("store");

		Map<String, Object> map = new HashMap<String, Object>();

		if (price != null && !"".equals(price.trim())) {
			ApiT_products t_product = new ApiT_products();
			t_product.setGoods_id(Long.valueOf(goods_id));
			t_product.setPrice(Util.getBigDecimal(price));
			t_product.setUnit(unit);
			t_product.setStore(Integer.valueOf(store));
			//

			apiT_productsService.insertT_products(t_product);
		}

		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * update
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_products")
	public void updateT_products(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String product_id = request.getParameter("product_id");
		String goods_id = request.getParameter("goods_id");
		String price = request.getParameter("price");
		String unit = request.getParameter("unit");
		String store = request.getParameter("store");

		Map<String, Object> map = new HashMap<String, Object>();

		if (price != null && !"".equals(price.trim())) {
			ApiT_products t_product = new ApiT_products();

			// 获取原数据
			t_product = apiT_productsService.findT_productsById(Long
					.valueOf(product_id));
			// 修改
			t_product.setGoods_id(Long.valueOf(goods_id));
			t_product.setPrice(Util.getBigDecimal(price));
			t_product.setUnit(unit);
			t_product.setStore(Integer.valueOf(store));
			//

			apiT_productsService.updateT_products(t_product);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "编辑成功！");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "检查失败！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_productsById")
	public void findT_productsById(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String product_id = request.getParameter("product_id");
		ApiT_products t_product = apiT_productsService
				.findT_productsById(Long.valueOf(product_id));

		// response
		response.setCharacterEncoding("UTF-8");
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "查询成功！");
		map.put("datas", t_product);
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据商品id查询货品详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findProductsByGoodsId")
	public void findProductsByGoodsId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String goods_id = request.getParameter("goods_id");

		try {
			List<Map<String, Object>> t_products = apiT_productsService
					.findProductsByGoodsId(Long.valueOf(goods_id));

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
			map.put("datas", t_products);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
